﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Yipusoft.Account.Model;
using Yipusoft.SixAcquaint.Model;
using Yipusoft.SixAcquaint.BLL;
using System.Collections.Generic;
using Yipusoft.Common.BLL;
using Yipusoft.Common.Model;
using Yipusoft.Account.BLL;

public partial class BasicBase_WaterSource_NewWater : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            UserInfo userInfo = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
            DepartmentInfo department = Department.GetDepartment(userInfo.DepartmentID);
            if (userInfo.DepartmentID.Substring(0, 4) == "1300")
            {
                DepartmentList.DataSource = Yipusoft.Account.BLL.Department.GetList(" (SUBSTRING(DepartmentID, 5, 3) = '000') and DepartmentID<>'1300000' ");
                DepartmentList.DataBind();
                DepartmentListBind();
            }
            else
            {
                DepartmentList.DataSource = Yipusoft.Account.BLL.Department.GetList(" DepartmentID= '" + department.DepartmentID.Substring(0, 4) + "000' ");
                DepartmentList.DataBind();
                DepartmentListBind();
            }

            //DepartmentList.SelectedValue = department.DepartmentID.Substring(0, 4) + "000";
            //UnitList.SelectedValue = department.DepartmentID;

            BindProvince();
            BindCities(ddlProvinces.SelectedValue);

            //BindDepartment("200800", "200804", "200801");
            if (Request["ID"] != null)
            {
                WaterInfo waterDetails = Water.GetWaterByID(Request["ID"]);
                txtBH.Text = waterDetails.BH.ToString();
                txtCaliber1.Text = waterDetails.KouJing1.ToString();
                txtCaliber2.Text = waterDetails.KouJing2.ToString();
                txtDrang.Text = waterDetails.Pressure.ToString();
                txtJingDu.Text = waterDetails.JingDu.ToString();
                txtWeiDu.Text = waterDetails.WeiDu.ToString();
                txtLocation.Text = waterDetails.Location;
                txtMemo.Text = waterDetails.Memo;
                txtPipe.Text = waterDetails.GuanJing.ToString();
                ddlGZh.SelectedValue = waterDetails.GuanZhuang;
                //RoadInfo roadDetails = Road.GetRoadByID(waterDetails.RoadID);
                if (waterDetails.RegionID == null || waterDetails.RegionID == "")
                {

                }
                else
                {
                    CodeItemInfo itemInfo = CodeItem.GetCodeItem(waterDetails.RegionID);
                    ddlProvinces.SelectedValue = itemInfo.ItemLevel <= 2 ? itemInfo.ParentID.Trim() : CodeItem.GetCodeItem(itemInfo.ParentID).ParentID.Trim();
                    BindCities(ddlProvinces.SelectedValue);
                    ddlCities.SelectedValue = waterDetails.RegionID;
                }
                RoadName.Text = waterDetails.RoadName;
                //bindRoads(roadDetails.RegionID);
                //ddlRoads.SelectedValue = waterDetails.RoadID;
                chkStatus.Checked = waterDetails.Workable;
                DepartmentList.SelectedValue = waterDetails.DepartmentID.Substring(0, 4) + "000";

                DepartmentListBind();
                UnitList.SelectedValue = waterDetails.DepartmentID.Trim();
            }
            else
            {

                DepartmentListBind();
                UnitList.SelectedValue = department.DepartmentID.Trim();
            }
            BindWater();
        }
    }

    void DepartmentListBind()
    {
        string strWhere = "departmentid like '" + DepartmentList.SelectedValue.Substring(0, 4) + "3%' or departmentid like '" + DepartmentList.SelectedValue.Substring(0, 4) + "1%' ";
        UnitList.DataSource = Department.GetList(strWhere);
        UnitList.DataBind();
    }

    protected void DepartmentList_SelectedIndexChanged1(object sender, EventArgs e)
    {
        DepartmentListBind();
        BindWater();
    }

    protected void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        //BindWater(ddlRoads.SelectedValue, 10, PageIndex);
    }
    private void BindWater()
    {
        string sql = "AddedDate>='" + DateTime.Today.ToString("yyyy-MM-dd") + "'";

        string departmentid = UnitList.SelectedValue;

        if (departmentid.Substring(4, 1) == "3")
        {
            sql += " and DepartmentID='" + departmentid + "'";

        }
        else
        {
            sql += " and Departmentid like '" + departmentid.Substring(0, 4) + "3%' ";
        }


        gvWater.DataSource = Water.GetWaterList(sql, "AddedDate", "Desc", 10, 0);
        gvWater.DataBind();
    }

    private void BindWater(string roadID, int pageSize, int pageIndex)
    {
        if (!panPage.Visible)
            panPage.Visible = true;
        int totalCount = Water.GetWaterCountByWhere("RoadID='" + roadID + "'");
        ListPageContral1.ItemCount = totalCount;
        gvWater.DataSource = Water.GetWaterList("RoadID='" + roadID + "'", "AddedDate", "Desc", pageSize, pageIndex);
        gvWater.DataBind();

    }

    private void BindProvince()
    {
        ListItem blankLI = new ListItem("", "");
        ddlProvinces.Items.Add(blankLI);
        foreach (CodeItemInfo item in CodeItem.GetList("ParentID='SS000000' ORDER BY SeqID"))
        {
            ListItem li = new ListItem(item.ItemName, item.ItemID);
            ddlProvinces.Items.Add(li);
        }
    }

    protected void ddlProvinces_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindCities(ddlProvinces.SelectedValue);
    }

    private void BindCities(string province)
    {
        //ddlCities.DataSource = CodeItem.GetList("ParentID='"+province+"' Order by SeqID");
        //ddlCities.DataTextField = "ItemName";
        //ddlCities.DataValueField = "ItemID";
        //ddlCities.DataBind();
        ddlCities.Items.Clear();
        if (province.Trim() == "")
        {
            ListItem blankLI = new ListItem("", "");
            ddlCities.Items.Add(blankLI);
        }
        else
        {
            foreach (CodeItemInfo item in CodeItem.GetList("ParentID='" + province + "' Order by SeqID"))
            {
                ListItem li = new ListItem(item.ItemName, item.ItemID);
                ddlCities.Items.Add(li);
                foreach (CodeItemInfo subItem in CodeItem.GetList("ParentID='" + item.ItemID + "' Order by SeqID"))
                    ddlCities.Items.Add(new ListItem("\u3000" + subItem.ItemName, subItem.ItemID));
            }
        }
    }

    //private void BindDepartment(string permissionAdmin, string permissionLevel, string permissionUnit)
    //{
    //    if (Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, permissionAdmin))
    //    {
    //        foreach (DepartmentInfo department in Department.GetList("RIGHT(DepartmentID, 2) = '00'"))
    //        {
    //            ddlDepartment.Items.Add(new ListItem(department.FullName, department.DepartmentID));
    //        }
    //    }
    //    else
    //    {
    //        ddlDepartment.DataSource = GetDepartmentList(Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID, permissionLevel, permissionUnit);
    //        ddlDepartment.DataTextField = "FullName";
    //        ddlDepartment.DataValueField = "DepartmentID";
    //        ddlDepartment.DataBind();

    //    }
    //}
    private List<DepartmentInfo> GetDepartmentList(string departmentID, string permissionLevel, string permissionUnit)
    {
        List<DepartmentInfo> departmentList = new List<DepartmentInfo>();
        if (Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, permissionLevel) || Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, permissionUnit))
            departmentList = Department.GetList("DepartmentID='" + departmentID.Substring(0, 4) + "000'");
        return departmentList;
    }


    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(Request["ID"]))
        {
            WaterInfo waterDetails = new WaterInfo();
            waterDetails.RegionID = ddlCities.SelectedValue;
            waterDetails.RoadName = RoadName.Text;
            waterDetails.BH = Convert.ToDecimal(txtBH.Text);
            waterDetails.Pressure = Convert.ToDecimal(txtDrang.Text == "" ? "0" : txtDrang.Text);
            waterDetails.Location = txtLocation.Text;
            waterDetails.GuanZhuang = ddlGZh.SelectedValue;
            waterDetails.DepartmentID = UnitList.SelectedValue;
            waterDetails.GuanJing = Convert.ToDecimal(txtPipe.Text == "" ? "0" : txtPipe.Text);
            waterDetails.KouJing1 = Convert.ToDecimal(txtCaliber1.Text == "" ? "0" : txtCaliber1.Text);
            waterDetails.KouJing2 = Convert.ToDecimal(txtCaliber2.Text == "" ? "0" : txtCaliber2.Text);
            waterDetails.JingDu = Convert.ToDouble(txtJingDu.Text == "" ? "0" : txtJingDu.Text);
            waterDetails.WeiDu = Convert.ToDouble(txtWeiDu.Text == "" ? "0" : txtWeiDu.Text);
            waterDetails.Workable = chkStatus.Checked;
            waterDetails.Memo = txtMemo.Text;
            waterDetails.AddedDate = DateTime.Now;
            if (Water.Insert(waterDetails) == 1)
            {
                lblInfo.Text = "添加成功。";
                BindWater();
            }
            else
            {
                lblInfo.Text = "添加失败！";
            }
        }
        else
        {
            WaterInfo waterDetails = Water.GetWaterByID(Request["ID"]);
            waterDetails.RegionID = ddlCities.SelectedValue;
            waterDetails.RoadName = RoadName.Text;
            waterDetails.BH = Convert.ToDecimal(txtBH.Text);
            waterDetails.Pressure = Convert.ToDecimal(txtDrang.Text == "" ? "0" : txtDrang.Text);
            waterDetails.Location = txtLocation.Text;
            waterDetails.GuanZhuang = ddlGZh.SelectedValue;
            waterDetails.DepartmentID = UnitList.SelectedValue;
            waterDetails.GuanJing = Convert.ToDecimal(txtPipe.Text == "" ? "0" : txtPipe.Text);
            waterDetails.KouJing1 = Convert.ToDecimal(txtCaliber1.Text == "" ? "0" : txtCaliber1.Text);
            waterDetails.KouJing2 = Convert.ToDecimal(txtCaliber2.Text == "" ? "0" : txtCaliber2.Text);
            waterDetails.JingDu = Convert.ToDouble(txtJingDu.Text == "" ? "0" : txtJingDu.Text);
            waterDetails.WeiDu = Convert.ToDouble(txtWeiDu.Text == "" ? "0" : txtWeiDu.Text);
            waterDetails.Workable = chkStatus.Checked;
            waterDetails.Memo = txtMemo.Text;
            waterDetails.AddedDate = DateTime.Now;
            if (Water.Update(waterDetails))
            {
                lblInfo.Text = "更新成功。";
                BindWater();
            }
            else
                lblInfo.Text = "更新失败。";
        }
    }
    protected void ddlCities_SelectedIndexChanged(object sender, EventArgs e)
    {
        //bindRoads(ddlCities.SelectedValue);
    }
    protected void ddlRoads_SelectedIndexChanged(object sender, EventArgs e)
    {
        ListPageContral1.SetFirstPage();
        //BindWater(ddlRoads.SelectedValue, 10, 0);
    }

    protected void gvWater_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "200800") || Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "200803"))
        {
            if (Water.Delete(gvWater.DataKeys[e.RowIndex].Value.ToString()))
            {
                BindWater();
            }
        }
        else
        {
            Response.Write("<script>alert('没有权限！');history.back(0);</script>");
            return;
        }
    }
    protected void gvWater_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblID = e.Row.FindControl("lblID") as Label;
            lblID.Text = Convert.ToString(gvWater.PageSize * ListPageContral1.CurrentPageIndex + (e.Row.RowIndex + 1));
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }
    }
    protected void UnitList_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindWater();
    }
}
